﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{    
    public static class DirectionExtensions
    {
        public static string DirectionText(this GeneralDirection dir)
        {
            switch (dir)
            {
                case GeneralDirection.N: return "North";
                case GeneralDirection.S: return "South";
                case GeneralDirection.E: return "East";
                case GeneralDirection.W: return "West";
                default:
                    return dir.ToString();
            }
        }

        public static DirectionInt GetDirectionVector(this GeneralDirection dir)
        {
            return DirectionInt.VectorsForDirections[(int)dir];
        }

        /// <summary>
        /// Determines if two directions are somewhat the same (N vs. NE vs. NW, etc)
        /// </summary>		
        public static bool IsGeneralSameDirection(this GeneralDirection dir1, GeneralDirection dir2)
        {
            switch (dir1)
            {
                case GeneralDirection.N: return dir2.IsInSet(GeneralDirection.N, GeneralDirection.NE, GeneralDirection.NW);
                case GeneralDirection.S: return dir2.IsInSet(GeneralDirection.S, GeneralDirection.SE, GeneralDirection.SW);
                case GeneralDirection.E: return dir2.IsInSet(GeneralDirection.E, GeneralDirection.NE, GeneralDirection.SE);
                case GeneralDirection.W: return dir2.IsInSet(GeneralDirection.W, GeneralDirection.NW, GeneralDirection.SW);
                case GeneralDirection.NE: return dir2.IsInSet(GeneralDirection.NE, GeneralDirection.E, GeneralDirection.N);
                case GeneralDirection.NW: return dir2.IsInSet(GeneralDirection.NW, GeneralDirection.W, GeneralDirection.N);
                case GeneralDirection.SE: return dir2.IsInSet(GeneralDirection.SE, GeneralDirection.E, GeneralDirection.S);
                case GeneralDirection.SW: return dir2.IsInSet(GeneralDirection.SW, GeneralDirection.W, GeneralDirection.S);
            }

            return false;
        }

        /// <summary>
        /// Returns a list of directions in a comma-delimited direction string
        /// </summary>
        public static List<GeneralDirection> ReadDirectionList(this string s)
        {
            try
            {
                List<GeneralDirection> directionList = new List<GeneralDirection>();
                string[] dirs = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                directionList.AddRange(from direction in dirs
                                       let dir = direction.Trim()
                                       let generalDir = (GeneralDirection)Enum.Parse(typeof(GeneralDirection), dir, true)
                                       select generalDir);

                return directionList;
            }
            catch (Exception ex)
            {
                throw new Exception("Could not parse direction list: " + s, ex);
            }
        }

        public static GeneralDirection ReverseDirection(this GeneralDirection dir)
        {            
            return (GeneralDirection)((ReverseDirections)((int)dir));
        }
    }
}